package com.androidx.p013lv.mine.activity;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import com.androidx.p013lv.base.bean.UserInfo;
import com.androidx.p013lv.base.http.BaseRes;
import com.androidx.p013lv.base.p015ui.BaseActivity;
import com.androidx.p013lv.base.utils.NetUtil;
import com.androidx.p013lv.base.utils.SpUtils;
import com.androidx.p013lv.base.utils.ToastUtils;
import com.androidx.p013lv.base.utils.UiUtils;
import com.androidx.p013lv.mine.C1207R;
import com.androidx.p013lv.mine.bean.ExchangeVipBean;
import com.androidx.p013lv.mine.bean.RecodeBean;
import com.androidx.p013lv.mine.databinding.ActivityExchangeCenterLayoutBinding;
import com.androidx.p013lv.mine.model.ExchangeCenterModel;
import com.gyf.immersionbar.ImmersionBar;

/* renamed from: com.androidx.lv.mine.activity.ExchangeCenterActivity */
public class ExchangeCenterActivity extends BaseActivity<ActivityExchangeCenterLayoutBinding> {
    private ExchangeCenterModel model;

    /* access modifiers changed from: protected */
    @Override // com.androidx.p013lv.base.p015ui.BaseActivity
    public void initImmersionBar() {
        super.initImmersionBar();
        ImmersionBar.with(this).titleBar(((ActivityExchangeCenterLayoutBinding) this.binding).toolbar).init();
    }

    @Override // com.androidx.p013lv.base.p015ui.BaseActivity
    public int setLayout() {
        return C1207R.C1212layout.activity_exchange_center_layout;
    }

    /* access modifiers changed from: protected */
    @Override // com.androidx.p013lv.base.p015ui.BaseActivity
    public void initView() {
        super.initView();
        this.model = (ExchangeCenterModel) new ViewModelProvider(this).get(ExchangeCenterModel.class);
        ((ActivityExchangeCenterLayoutBinding) this.binding).setOnClick(new OnClick());
        ((ActivityExchangeCenterLayoutBinding) this.binding).setStatus(0);
        ((ActivityExchangeCenterLayoutBinding) this.binding).edContent.addTextChangedListener(new TextWatcher() {
            /* class com.androidx.p013lv.mine.activity.ExchangeCenterActivity.C12341 */

            public void afterTextChanged(Editable editable) {
            }

            public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
            }

            public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
                if (charSequence.length() == 6) {
                    ExchangeCenterActivity.this.redemptionCode(charSequence.toString());
                }
                if (charSequence.length() > 0) {
                    ((ActivityExchangeCenterLayoutBinding) ExchangeCenterActivity.this.binding).setStatus(1);
                } else {
                    ((ActivityExchangeCenterLayoutBinding) ExchangeCenterActivity.this.binding).setStatus(0);
                }
            }
        });
        this.model.dataRecode().observe(this, new Observer<BaseRes<RecodeBean>>() {
            /* class com.androidx.p013lv.mine.activity.ExchangeCenterActivity.C12352 */

            public void onChanged(BaseRes<RecodeBean> baseRes) {
                if (baseRes.getCode() == 200) {
                    RecodeBean data = baseRes.getData();
                    if (data == null) {
                        ToastUtils.getInstance().showSigh("兌換碼無效");
                    } else {
                        ((ActivityExchangeCenterLayoutBinding) ExchangeCenterActivity.this.binding).setRecode(data);
                    }
                } else {
                    ToastUtils.getInstance().showSigh(baseRes.getMsg());
                }
            }
        });
        this.model.dataCommit().observe(this, new Observer<BaseRes<ExchangeVipBean>>() {
            /* class com.androidx.p013lv.mine.activity.ExchangeCenterActivity.C12363 */

            public void onChanged(BaseRes<ExchangeVipBean> baseRes) {
                if (baseRes.getCode() == 200) {
                    ((ActivityExchangeCenterLayoutBinding) ExchangeCenterActivity.this.binding).getRecode().setStatus(0);
                    ToastUtils.getInstance().showCorrect("兌換成功");
                    UserInfo userInfo = SpUtils.getInstance().getUserInfo();
                    userInfo.setFreeWatches(-1);
                    userInfo.setExpiredVip(baseRes.getData().getExpiredVip());
                    SpUtils.getInstance().setUserInfo(userInfo);
                    return;
                }
                ToastUtils.getInstance().showWrong(baseRes.getMsg());
            }
        });
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void redemptionCode(String str) {
        if (!NetUtil.isNetworkAvailable()) {
            ToastUtils.getInstance().showSigh(UiUtils.getString(C1207R.string.hit_no_net));
        } else {
            this.model.getRecodeStatus(str, this);
        }
    }

    /* renamed from: com.androidx.lv.mine.activity.ExchangeCenterActivity$OnClick */
    public class OnClick {
        public OnClick() {
        }

        public void btnBack() {
            ExchangeCenterActivity.this.finish();
        }

        public void btnCommit() {
            if (!ExchangeCenterActivity.this.isOnClick()) {
                if (!NetUtil.isNetworkAvailable()) {
                    ToastUtils.getInstance().showWrong(UiUtils.getString(C1207R.string.hit_no_net));
                    return;
                }
                String trim = ((ActivityExchangeCenterLayoutBinding) ExchangeCenterActivity.this.binding).edContent.getText().toString().trim();
                if (TextUtils.isEmpty(trim)) {
                    ToastUtils.getInstance().showSigh("請輸入兌換碼");
                } else if (((ActivityExchangeCenterLayoutBinding) ExchangeCenterActivity.this.binding).getRecode() == null) {
                    ToastUtils.getInstance().showSigh("兌換碼無效");
                } else if (((ActivityExchangeCenterLayoutBinding) ExchangeCenterActivity.this.binding).getRecode().getStatus() != 1) {
                    ToastUtils.getInstance().showSigh("兌換碼已被使用");
                } else {
                    ExchangeCenterActivity.this.model.commitRecode(trim, ExchangeCenterActivity.this);
                }
            }
        }
    }

    /* access modifiers changed from: protected */
    @Override // androidx.appcompat.app.AppCompatActivity, androidx.fragment.app.FragmentActivity, com.androidx.p013lv.base.p015ui.BaseActivity
    public void onDestroy() {
        ExchangeCenterModel exchangeCenterModel = this.model;
        if (exchangeCenterModel != null) {
            exchangeCenterModel.cancelHttp();
        }
        super.onDestroy();
    }
}
